package com.digitalchina.lxnsy.activity.old;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.apkfuns.logutils.LogUtils;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessEvaluate;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.GoodsEvaluateVO;
import com.digitalchina.lxnsy.entity.OrderListVO;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：白彦东
 * 创建时间：2017/1/18 14:15
 * 修改备注：
 * ================================================
 */

public class EvaluateActivity extends BaseActivity {

    @Bind(R.id.evaluate_tv_merchants_name)
    TextView evaluateTvMerchantsName;
    @Bind(R.id.merchant_iv_star1)
    ImageView merchantIvStar1;
    @Bind(R.id.merchant_iv_star2)
    ImageView merchantIvStar2;
    @Bind(R.id.merchant_iv_star3)
    ImageView merchantIvStar3;
    @Bind(R.id.merchant_iv_star4)
    ImageView merchantIvStar4;
    @Bind(R.id.merchant_iv_star5)
    ImageView merchantIvStar5;
    @Bind(R.id.service_iv_star1)
    ImageView serviceIvStar1;
    @Bind(R.id.service_iv_star2)
    ImageView serviceIvStar2;
    @Bind(R.id.service_iv_star3)
    ImageView serviceIvStar3;
    @Bind(R.id.service_iv_star4)
    ImageView serviceIvStar4;
    @Bind(R.id.service_iv_star5)
    ImageView serviceIvStar5;
    @Bind(R.id.evaluation_product_item_layout)
    LinearLayout evaluationProductItemLayout;
    @Bind(R.id.evaluate_ck_anonymous)
    CheckBox evaluateCkAnonymous;
    @Bind(R.id.evaluate_btn_publish)
    TextView evaluateBtnPublish;
    @Bind(R.id.evaluate_bottom_layout)
    LinearLayout evaluateBottomLayout;

    private OrderListVO orderListVO; // 订单
    private int score = 1; // 商品满意度评分
    private int accordStore = 1;// 符合度评分
    private int serviceScore = 1;// 服务态度评分
    private String isAnonymity = "1";// 是否匿名 1 是 0否
    private List<Integer> scoreList = new ArrayList<>();
    private List<EditText> editTextsList = new ArrayList<>();
    private int goodsListSize;
    private int msgWhat;

    private ImageView ivStar1;
    private ImageView ivStar2;
    private ImageView ivStar3;
    private ImageView ivStar4;
    private ImageView ivStar5;

    @Override
    public void setLayout() {
        setTitle("评价");
        setContentLayout(R.layout.activity_evaluate);
    }

    @Override
    public void initMember() {
        //TODO 获取订单信息数据
        if (getIntent().hasExtra("OrderListVO"))
            orderListVO = (OrderListVO) getIntent().getSerializableExtra("OrderListVO");
        goodsListSize = orderListVO.getOrderItems().size();
        initProudctEvaluateView(goodsListSize);
    }

    /**
     * 初始化商品评价视图
     *
     * @param proudctNum 商品数量
     */
    private void initProudctEvaluateView(int proudctNum) {
        for (int i = 0; i < proudctNum; i++) {

            scoreList.add(i, score);

            View productEvaluation = LayoutInflater.from(this).inflate(R.layout.item_evaluate_product, null);

            SimpleDraweeView ivPicture = (SimpleDraweeView) productEvaluation.findViewById(R.id.item_evaluate_product_iv_images);
            ivStar1 = (ImageView) productEvaluation.findViewById(R.id.product_iv_star1);
            ivStar2 = (ImageView) productEvaluation.findViewById(R.id.product_iv_star2);
            ivStar3 = (ImageView) productEvaluation.findViewById(R.id.product_iv_star3);
            ivStar4 = (ImageView) productEvaluation.findViewById(R.id.product_iv_star4);
            ivStar5 = (ImageView) productEvaluation.findViewById(R.id.product_iv_star5);
            EditText editText = (EditText) productEvaluation.findViewById(R.id.item_evaluate_product_content);
            editTextsList.add(editText);

            FrescoUtil.showImageSmall(orderListVO.getOrderItems().get(i).getProductImg(), ivPicture);//商品图片

            final int position = i;

            ivStar1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setStar(position, 1);
                }
            });
            ivStar2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setStar(position, 2);
                }
            });
            ivStar3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setStar(position, 3);
                }
            });
            ivStar4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setStar(position, 4);
                }
            });
            ivStar5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setStar(position, 5);
                }
            });

            evaluationProductItemLayout.addView(productEvaluation);

        }
    }

    /**
     * 设置商品评价星数
     *
     * @param pos 存储位置
     * @param num 存储星数
     */
    private void setStar(int pos, int num) {
        score = num;
        scoreList.set(pos, score);
        setProductStar(score);
    }

    /**
     * 设置商品评论星数
     *
     * @param score
     */
    private void setProductStar(int score) {
        switch (score) {
            case 1:
                ivStar1.setImageResource(R.drawable.icon_average_star_solid);
                ivStar2.setImageResource(R.drawable.icon_average_star_hollow);
                ivStar3.setImageResource(R.drawable.icon_average_star_hollow);
                ivStar4.setImageResource(R.drawable.icon_average_star_hollow);
                ivStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 2:
                ivStar1.setImageResource(R.drawable.icon_average_star_solid);
                ivStar2.setImageResource(R.drawable.icon_average_star_solid);
                ivStar3.setImageResource(R.drawable.icon_average_star_hollow);
                ivStar4.setImageResource(R.drawable.icon_average_star_hollow);
                ivStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 3:
                ivStar1.setImageResource(R.drawable.icon_average_star_solid);
                ivStar2.setImageResource(R.drawable.icon_average_star_solid);
                ivStar3.setImageResource(R.drawable.icon_average_star_solid);
                ivStar4.setImageResource(R.drawable.icon_average_star_hollow);
                ivStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 4:
                ivStar1.setImageResource(R.drawable.icon_average_star_solid);
                ivStar2.setImageResource(R.drawable.icon_average_star_solid);
                ivStar3.setImageResource(R.drawable.icon_average_star_solid);
                ivStar4.setImageResource(R.drawable.icon_average_star_solid);
                ivStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 5:
                ivStar1.setImageResource(R.drawable.icon_average_star_solid);
                ivStar2.setImageResource(R.drawable.icon_average_star_solid);
                ivStar3.setImageResource(R.drawable.icon_average_star_solid);
                ivStar4.setImageResource(R.drawable.icon_average_star_solid);
                ivStar5.setImageResource(R.drawable.icon_average_star_solid);
                break;
        }
    }

    @Override
    public void addListener() {
        evaluateCkAnonymous.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked)
                    isAnonymity = "1";// 匿名评价
                else
                    isAnonymity = "0";
            }
        });

        evaluateBtnPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<GoodsEvaluateVO> evaluateList = new ArrayList<>();// 商品评价
                // 评价商品信息
                for (int i = 0; i < goodsListSize; i++) {
                    GoodsEvaluateVO goodsEvaluateVO = new GoodsEvaluateVO();
                    goodsEvaluateVO.setProductId(orderListVO.getOrderItems().get(i).getProductId());
                    goodsEvaluateVO.setContent(editTextsList.get(i).getText().toString());
                    goodsEvaluateVO.setScore(String.valueOf(scoreList.get(i)));
                    goodsEvaluateVO.setAtrrName(orderListVO.getOrderItems().get(i).getAttrName());
                    evaluateList.add(goodsEvaluateVO);
                }

                String evaluateProductStr = JSON.toJSONString(evaluateList, true);

                LogUtils.e(accordStore);
                LogUtils.e(serviceScore);
                LogUtils.e(isAnonymity);
                LogUtils.e(evaluateProductStr);

                BusinessEvaluate.mobileEvaluateOrder(EvaluateActivity.this, orderListVO.getOrderNo(),
                        String.valueOf(accordStore), String.valueOf(serviceScore), isAnonymity, evaluateProductStr, mHandler);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_EVALUATE_ORDER_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_EVALUATE_ORDER_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        //TODO 评价成功处理
        finish();
    }

    @Override
    public void rightOnClick() {

    }


    @OnClick({R.id.merchant_iv_star1, R.id.merchant_iv_star2, R.id.merchant_iv_star3, R.id.merchant_iv_star4, R.id.merchant_iv_star5, R.id.service_iv_star1, R.id.service_iv_star2, R.id.service_iv_star3, R.id.service_iv_star4, R.id.service_iv_star5, R.id.evaluate_btn_publish})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.merchant_iv_star1:
                setMerchantStar(1);
                break;
            case R.id.merchant_iv_star2:
                setMerchantStar(2);
                break;
            case R.id.merchant_iv_star3:
                setMerchantStar(3);
                break;
            case R.id.merchant_iv_star4:
                setMerchantStar(4);
                break;
            case R.id.merchant_iv_star5:
                setMerchantStar(5);
                break;
            case R.id.service_iv_star1:
                setServiceStar(1);
                break;
            case R.id.service_iv_star2:
                setServiceStar(2);
                break;
            case R.id.service_iv_star3:
                setServiceStar(3);
                break;
            case R.id.service_iv_star4:
                setServiceStar(4);
                break;
            case R.id.service_iv_star5:
                setServiceStar(5);
                break;
            case R.id.evaluate_btn_publish:
                break;
        }
    }

    /**
     * 设置商品符合度评分
     *
     * @param score
     */
    private void setMerchantStar(int score) {
        accordStore = score;
        switch (score) {
            case 1:
                merchantIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar2.setImageResource(R.drawable.icon_average_star_hollow);
                merchantIvStar3.setImageResource(R.drawable.icon_average_star_hollow);
                merchantIvStar4.setImageResource(R.drawable.icon_average_star_hollow);
                merchantIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 2:
                merchantIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar3.setImageResource(R.drawable.icon_average_star_hollow);
                merchantIvStar4.setImageResource(R.drawable.icon_average_star_hollow);
                merchantIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 3:
                merchantIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar3.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar4.setImageResource(R.drawable.icon_average_star_hollow);
                merchantIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 4:
                merchantIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar3.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar4.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 5:
                merchantIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar3.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar4.setImageResource(R.drawable.icon_average_star_solid);
                merchantIvStar5.setImageResource(R.drawable.icon_average_star_solid);
                break;
        }
    }

    /**
     * 设置商户服务态度星数
     *
     * @param score
     */
    private void setServiceStar(int score) {
        serviceScore = score;
        switch (score) {
            case 1:
                serviceIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar2.setImageResource(R.drawable.icon_average_star_hollow);
                serviceIvStar3.setImageResource(R.drawable.icon_average_star_hollow);
                serviceIvStar4.setImageResource(R.drawable.icon_average_star_hollow);
                serviceIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 2:
                serviceIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar3.setImageResource(R.drawable.icon_average_star_hollow);
                serviceIvStar4.setImageResource(R.drawable.icon_average_star_hollow);
                serviceIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 3:
                serviceIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar3.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar4.setImageResource(R.drawable.icon_average_star_hollow);
                serviceIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 4:
                serviceIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar3.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar4.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar5.setImageResource(R.drawable.icon_average_star_hollow);
                break;
            case 5:
                serviceIvStar1.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar2.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar3.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar4.setImageResource(R.drawable.icon_average_star_solid);
                serviceIvStar5.setImageResource(R.drawable.icon_average_star_solid);
                break;
        }
    }
}
